const {createAlipayTrade, getOauthToken} = require('../config/alipay')

class PaymentController {
    async alipayPayment(ctx) {
        const {authCode, openId} = ctx.request.body;
        console.log(authCode)
        try {
            // const {money, subject, trade_no} = ctx.request.body;
            // if (!(0.01 < money < 100000000)) {  //支付宝小程序规定范围
            //     throw({message: '订单总金额超出范围'})
            // }
            // if (typeof money === "number") {
            //     if (money.toString().split('.') && money.toString().split('.')[1].length !== 2) {
            //         throw({message: '金额需要精确到小数点后两位'})
            //     }
            // }
            const userInfo = await getOauthToken(authCode)
            console.log(userInfo)
            const res = await createAlipayTrade(ctx, userInfo.openId, openId)
            ctx.body = {
                status: 0,
                msg: '成功',
                data: res
            }
        } catch (e) {
            ctx.body = {
                status: 1,
                msg: e.message
            }
        }


    }
}

module.exports = new PaymentController();
